//
//  TransferDepaCancelView.swift
//  MedicalUnion
//
//  Created by 来 on 2019/9/25.
//  Copyright © 2019 William_Xue. All rights reserved.
//

import UIKit

class TransferDepaCancelView: UIView {
    
    var bgView: UIView!
    var label: UILabel!
    var textView: UITextView!
    var bottomView: UIView!
    
    var titleStr: String!
    
    var TransferConfirmHandoverViewBlock: ((String) -> Void)!

    func lgf(_ SV: UIView, _ title: String, _ text: String, _ callBackBlock:((String) -> Void)?) -> Void {
        SV.addSubview(self)
        lgf_FillSuperview()
        titleStr = text
        TransferConfirmHandoverViewBlock = callBackBlock
        config()
        label.text = title
        bgView.transform = CGAffineTransform.init(scaleX: 0.8, y: 0.8)
        DispatchQueue.main.async {
            UIView.animate(withDuration: 0.2) {
                self.bgView.transform = CGAffineTransform.identity
                self.backgroundColor = UIColor.black.withAlphaComponent(0.2)
            }
        }
    }
    
    func config() -> Void {
        bgView = UIView.init()
        bgView.layer.cornerRadius = 10
        bgView.clipsToBounds = true
        bgView.backgroundColor = EWEDarkMode.white()
        addSubview(bgView)
        bgView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(0.7)
            make.height.equalTo(200)
        }
        
        bottomView = UIView.init()
        bgView.addSubview(bottomView)
        bottomView.snp.makeConstraints { (make) in
            make.bottom.left.right.equalToSuperview()
            make.height.equalTo(50.lgf_H())
        }
        
        let topView = UIView.init()
        bgView.addSubview(topView)
        topView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.bottom.equalTo(bottomView.snp.top)
        }
        
        let line = UIView.init()
        line.backgroundColor = EWEDarkMode.color_F1F2F4()
        bgView.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalTo(bottomView.snp.top)
            make.height.equalTo(0.5)
        }
        
        let leftButton = LGFLeftRowButton.lgf(text: "取消", font: UIFont.systemFont(ofSize: 16), textColor: UIColor.init(lgf_HexString: "333333")!, leftLineHeight: 50)
        leftButton.button.addTarget(self, action: #selector(leftButtonSelect(_:)), for: .touchUpInside)
        bottomView.addSubview(leftButton)
        leftButton.snp.makeConstraints { (make) in
            make.left.top.bottom.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(0.5)
        }
        
        let rightButton = UIButton.init(type: .custom)
        rightButton.setTitle("确认拒绝", for: .normal)
        rightButton.titleLabel!.font = UIFont.systemFont(ofSize: 16)
        rightButton.setTitleColor(UIColor.init(lgf_HexString: "3360E6"), for: .normal)
        rightButton.addTarget(self, action: #selector(rightButtonSelect(_:)), for: .touchUpInside)
        bottomView.addSubview(rightButton)
        rightButton.snp.makeConstraints { (make) in
            make.right.top.bottom.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(0.5)
        }

        label = UILabel.init()
        label.textColor = UIColor.init(lgf_HexString: "111111")
        label.textAlignment = .center
        label.font = UIFont.systemFont(ofSize: 16)
        topView.addSubview(label)
        label.snp.makeConstraints { (make) in
            make.right.left.equalToSuperview()
            make.top.equalTo(15)
            make.height.equalTo(20)
        }
        
        textView = UITextView.init()
        textView.textColor = UIColor.init(lgf_HexString: "111111")
        textView.layer.borderColor = EWEDarkMode.color_F1F2F4().cgColor
        textView.layer.borderWidth = 1
        textView.layer.cornerRadius = 5
        textView.font = UIFont.systemFont(ofSize: 16)
        topView.addSubview(textView)
        textView.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.top.equalTo(label.snp.bottom).offset(15)
            make.bottom.equalToSuperview().offset(-15)
        }
        textView.becomeFirstResponder()
    }
    
    @objc func leftButtonSelect(_ sender: UIButton) -> Void {
        remove()
    }
    
    @objc func rightButtonSelect(_ sender: UIButton) -> Void {
        TransferConfirmHandoverViewBlock(textView.text)
        remove()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        textView.resignFirstResponder()
    }
    
    func remove() -> Void {
        UIView.animate(withDuration: 0.2, animations: {
            self.bgView.transform = CGAffineTransform.init(scaleX: 0.8, y: 0.8)
            self.bgView.alpha = 0.0
            self.backgroundColor = UIColor.black.withAlphaComponent(0.0)
        }) { (finish) in
            self.removeFromSuperview()
        }
    }
    
}

